package com.app.newiptv.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;

import com.app.newiptv.bean.LiveListBean;
import com.app.newiptv.service.NetChangeService;
import com.app.newiptv.utils.greendao.GreenDaoManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.danikula.videocache.HttpProxyCacheServer;
import com.zhy.http.okhttp.OkHttpUtils;
import org.xutils.x;

import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;

/**
 * Created by Shinelon on 2017/9/14.
 */

public class MyApplication extends Application {
    private HttpProxyCacheServer proxy;
    public static MyApplication mApp;
    public static Typeface face;
    public static List<LiveListBean> liveListBeen;
    private static Context context;
    public static final int COLLECTIION = 99999; //收藏
    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
        context = getContext();
        liveListBeen = new ArrayList<>();
        face = Typeface.createFromAsset(getApplicationContext().getAssets(), "hei_jian.ttf");
        startService();
        initGreenDao();
        initOkGo();
    }
    public static HttpProxyCacheServer getProxy(Context context) {
        MyApplication app = (MyApplication) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .maxCacheSize(256 * 1024 * 1024)       // 256 MB for cache
                .build();
    }
    public static Context getContext()
    {
        return context;
    }
    public void initGreenDao() {
        GreenDaoManager.getInstance();
    }


    private void startService()
    {
        Intent netChangeService = new Intent(this, NetChangeService.class);
//        netChangeService.setAction("com.app.newiptv.service.NetChangeService");
        startService(netChangeService);
    }

    public void initOkGo() {
        //必须调用初始化
        initOkHttp();
        x.Ext.init(this);
    }

    private void initOkHttp() {
        final X509TrustManager trustAllCert = new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[]{};
            }
        };
        try {
            final SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{trustAllCert}, null);
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
        OkHttpClient client = new OkHttpClient.Builder().sslSocketFactory(new SSL(trustAllCert), trustAllCert).build();
                Glide.get(this).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
        OkHttpUtils.initClient(client);
    }

}
